<?xml version="1.0" encoding="ISO-8859-1"?>

<!--
    Document   : manual.xsl
    Created on : December 25, 2013, 8:42 AM
    Author     : roberto
    Description:
        Generate a human-readable html document from a palette.xml
-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes" encoding="ISO-8859-1"/>
    
    <!-- 
    <xsl:variable name="imgdirectory">../resources/icons/</xsl:variable>
    -->
    
    <xsl:variable name="imgdirectory">icons/</xsl:variable>

    <xsl:template match="/">
        
        <html>
            <head>
                <title>Bobbietalk: Elenco dei blocchi disponibili</title>
            </head>
            <body>
                <h1>Bobbietalk: Elenco dei blocchi disponibili</h1>
                <table border="1">
                    <tr>
                        <th>Icona</th>
                        <th>Tipo del Blocco</th>
                        <th>Nome del Blocco</th>
                        <th>Descrizione</th>
                        <th>Parametri</th>
                    </tr>
                    <xsl:apply-templates select="palette/blockClass"/>
                </table>

            </body>
        </html>
    </xsl:template>
    
    <xsl:template match="blockClass">
        <tr>
            <td>
                <xsl:element name="img">
                    <xsl:attribute name="src">
                        <xsl:value-of select="$imgdirectory"/>
                        <xsl:value-of select="action-icon"/>.png</xsl:attribute>
                </xsl:element>
            </td>
            <td>
                <xsl:value-of select="type"/>
            </td>
            <td>
                <!-- xsl:value-of select="label[@lang='it']"/ -->
                
                <xsl:variable name="v1">
                    <xsl:call-template name="string-replace-all">
                        <xsl:with-param name="text" select="label[@lang='it']" />
                        <xsl:with-param name="replace" select="'&lt;html&gt;'" />
                        <xsl:with-param name="by" select="" />
                    </xsl:call-template>
                </xsl:variable>
                
                <xsl:variable name="v2">
                    <xsl:call-template name="string-replace-all">
                        <xsl:with-param name="text" select="$v1" />
                        <xsl:with-param name="replace" select="'&lt;/html&gt;'" />
                        <xsl:with-param name="by" select="" />
                    </xsl:call-template>
                </xsl:variable>
                
                <xsl:variable name="v3">
                    <xsl:call-template name="string-replace-all">
                        <xsl:with-param name="text" select="$v2" />
                        <xsl:with-param name="replace" select="'&lt;br/&gt;'" />
                        <xsl:with-param name="by" select="' '" />
                    </xsl:call-template>
                </xsl:variable>

                <xsl:value-of select="$v3"/>
                
            </td>
            <td>
                <xsl:value-of select="tooltip[@lang='it']"/>
            </td>
            <td>
                <xsl:if test="attributes/attribute[editable='true']">
                    <xsl:apply-templates select="attributes"/>
                </xsl:if>
                <xsl:if test="not(attributes/attribute[editable='true'])">
                    <xsl:text>- nessuno -</xsl:text>
                </xsl:if>
            </td>
        </tr>
    </xsl:template>

    <xsl:template match="attributes">
        <table>
            <xsl:apply-templates select="attribute[editable='true']"/>
        </table>
    </xsl:template>

    <xsl:template match="attribute">
        <tr>
            <td>
                <xsl:value-of select="type"/>
            </td>  
            <td>
                <xsl:value-of select="label[@lang='it']"/>
            </td>
            <td>
                <xsl:if test="units != ''">
                    [<xsl:value-of select="units"/>]
                </xsl:if>
            </td>
        </tr>
    </xsl:template>
    
    <!-- string replacement from http://geekswithblogs.net/Erik/archive/2008/04/01/120915.aspx -->
    
    <xsl:template name="string-replace-all">
        <xsl:param name="text" />
        <xsl:param name="replace" />
        <xsl:param name="by" />
        <xsl:choose>
            <xsl:when test="contains($text, $replace)">
                <xsl:value-of select="substring-before($text,$replace)" />
                <xsl:value-of select="$by" />
                <xsl:call-template name="string-replace-all">
                    <xsl:with-param name="text"
                                    select="substring-after($text,$replace)" />
                    <xsl:with-param name="replace" select="$replace" />
                    <xsl:with-param name="by" select="$by" />
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$text" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>
